//
// Created by jyc on 2021/10/7.
//
#include"Logger.h"
#include "ResourceManager.h"

namespace Bonnie{
    ResourceManager::SharedPtr ResourceManager::create()
    {
        return std::make_shared< ResourceManager>();
    }
    bool Bonnie::ResourceManager::registerField(const std::string &name, const Bonnie::RenderPassReflection::Field &field,
                                                uint32_t timePoint, const std::string &alias) {
        Bonnie_Assert(mName2Idx.find(name) == mName2Idx.end(), "already register");

        if (alias.empty()) {
            mName2Idx[name] = mReources.size();
            mReources.push_back({ field,{timePoint,timePoint} });
        }
        else {
            Bonnie_Assert(mName2Idx.find(alias) != mName2Idx.end(), "faild to find the source");
            auto idx = mName2Idx[alias];
            mName2Idx[name] = idx;

            if (!mReources[idx].field.isCompatiable(field))
                return false;

            mReources[idx].lifeTime.second = timePoint;
        }
        
    }
    void ResourceManager::clearRGResouce()
    {
        mName2Idx.clear();
        mReources.clear();
    }
}
